﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lm.Common.ExtensionMethods;

namespace Lm.Common.Auth.Account
{
    [Operation.Operation("{636A741D-E322-4D2D-9526-C28BC4ADF5A7}", "帐号管理员", "")]
    public class AccountAdmin:Operation.OperationBase
    {
        static AccountAdmin()
        { Operation.OperationBase.CreateOperation(typeof(AccountAdmin)); }

        public AccountAdmin(string token)
            : base(token)
        { }

        public void ConfirmAccount(string userAccount)
        {
            if (userAccount.IsNullOrBlank())
            { throw new Exception("帐号不能为空"); }
            using (var tran = new System.Transactions.TransactionScope())
            {
                var account = new AccountDal().GetByUserAccount(userAccount);
                if (account.GetStatus() != Model.RoleStatus.Draft)
                { throw new Exception("不是草稿状态，不能审核"); }
                account.SetStatus(Model.RoleStatus.Live);
                if (DAL.DALFactory.Create().Update<Model.Account>(account, t => t.Id == account.Id) != 1)
                { throw new Exception("审核帐号失败"); }
                tran.Complete();
            }
        }

        public void KillAccount(string userAccount)
        {
            if (userAccount.IsNullOrBlank())
            { throw new Exception("帐号不能为空"); }
            using (var tran = new System.Transactions.TransactionScope())
            {
                var account = new AccountDal().GetByUserAccount(userAccount);
                if (account.GetStatus() != Model.RoleStatus.Live)
                { throw new Exception("不是可用状态，不能作废"); }
                account.SetStatus(Model.RoleStatus.Death);
                if (DAL.DALFactory.Create().Update<Model.Account>(account, t => t.Id == account.Id) != 1)
                { throw new Exception("作废帐号失败"); }
                tran.Complete();
            }
        }

        public void Modify(Model.Account account, string roleCode)
        {
            account.TrimStringProperty();
            account.Id = Guid.NewGuid();
            if (account.UserAccount.IsNullOrBlank())
            { throw new Exception("帐号不能为空"); }
            if (account.Name.IsNullOrBlank())
            { throw new Exception("名称不能为空"); }
            if (account.Password.IsNullOrBlank() == false)
            { account.Password = new Lm.Common.Security.Cryptography.Md5Computer().Compute(account.Password); }
            if (roleCode.IsNullOrBlank())
            { throw new Exception("角色编号不能为空"); }
            using (var tran = new System.Transactions.TransactionScope())
            {
                var helper = DAL.DALFactory.Create();
                var data = new AccountDal().GetByUserAccount(account.UserAccount);
                if (data.GetStatus() != Model.RoleStatus.Live)
                { throw new Exception("帐号不是可用状态，不能修改"); }
                var role = new Role.RoleDal().GetByRoleCode(roleCode);
                if (role.GetRoleStatus() != Model.RoleStatus.Live)
                { throw new Exception("角色不是可用状态"); }
                data.RoleId = role.RoleId;
                data.Name = account.Name;
                if (account.Password.IsNullOrBlank() == false)
                { data.Password = account.Password; }
                data.Pause = account.Pause;
                data.Remark = account.Remark;
                if (helper.Update<Model.Account>(data, t => t.Id == data.Id) != 1)
                { throw new Exception("修改帐号失败"); }
                tran.Complete();
            }
        }

    }
}
